#include "system.hpp"
#include "gpio.hpp"
#include "Pin.hpp"

Pin *led = nullptr;

void delay(){
    for ( int m = 0; m < 1000; m++ ){
        for ( int n = 0; n < 1000; n++ ){
            __asm__("nop");
        }
    }
}

void Input_callback(void){
    led->toggle();
}

extern "C" int main(){
    Port *port_a = CH32V30X::Port::getInstance(0);
    Port *port_b = CH32V30X::Port::getInstance(1);
    port_a->open();
    port_b->open();
    led = port_a->getPin(15);
    Pin *input = port_b->getPin(0);
    Pin *output = port_b->getPin(1);
    led->setMode(Pin::Mode::Output);
    led->setDriveType(Pin::DriveType::PushPull);
    led->open();
    led->set(false);
    input->setMode(Pin::Mode::Input);
    input->setPullType(Pin::PullType::PullUp);
    input->open();
    input->registEvent(Pin::EventType::BothEdge,Input_callback);
    output->setMode(Pin::Mode::Output);
    output->setDriveType(Pin::DriveType::PushPull);
    output->open();
    while ( true ){
        output->toggle();
        System::delayMs(1000);
    }
}